package servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegisterServlet
 */
//@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        System.out.println("RegisterServlet   create .....");
    }

    @Override
    public void destroy() {
    	 System.out.println("RegisterServlet   destroy .....");
    }
    
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		ServletContext sc = request.getServletContext();
		
		if(request.getAttribute("count") != null) {
			int count = (int) request.getAttribute("count");
			request.setAttribute("count", count+1);
		}else {
			request.setAttribute("count", 1);
		}
		
		request.getRequestDispatcher("count.jsp").forward(request, response);
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8"); 
		String username = request.getParameter("username");
		System.out.println(username);
		
		String age = request.getParameter("age");  //null和空串的问题
		     
		System.out.println("age:"+age);
		String sex = request.getParameter("sex");
		System.out.println("性别："+sex);
		String Email = request.getParameter("email");
		System.out.println(Email);
		
		request.setAttribute("username", username);
		request.setAttribute("age", age);
		request.setAttribute("sex", sex);
		request.setAttribute("email", Email);
		
		//注册的用户信息写到数据库里
		//转发 服务器端跳转
//		request.getRequestDispatcher("/register_result.jsp").forward(request, response);
		//重定向  客户端跳转
		response.sendRedirect("register_result.jsp?username="+username + "&age="+age);
		
		//response.sendRedirect("https://www.baidu.com");
	}

}
